<template>
  <div id="addQuestionView">
    <a-form :model="form">
      <a-form-item :content-flex="false" :merge-props="false">
        <a-space direction="vertical">
          <a-form-item field="answer" label="answer">
            <MdEditor :value="form.answer" :handle-change="onAnswerChange" />
          </a-form-item>

          <a-form-item field="content" label="content">
            <MdEditor :value="form.content" :handle-change="onContentChange" />
          </a-form-item>

          <a-form-item field="title" label="title">
            <a-input
              v-model="form.title"
              placeholder="please enter your title..."
            />
          </a-form-item>

          <a-form-item field="tags" label="tags"
            ><a-input-tag
              :default-value="['test']"
              :style="{ width: '320px' }"
              placeholder="Please Choose tags..."
              v-model="form.tags"
              allow-clear
            />
          </a-form-item>

          <a-form-item field="timeLimit" label="timeLimit">
            <a-input-number
              :style="{ width: '320px' }"
              placeholder="please enter question timeLimit..."
              mode="button"
              size="large"
              v-model="form.judgeConfig.timeLimit"
            />
          </a-form-item>
          <a-form-item field="memoryLimit" label="memoryLimit">
            <a-input-number
              :style="{ width: '320px' }"
              placeholder="please enter question memoryLimit..."
              mode="button"
              size="large"
              v-model="form.judgeConfig.memoryLimit"
            />
          </a-form-item>
          <a-form-item field="stackLimit" label="stackLimit">
            <a-input-number
              :style="{ width: '320px' }"
              placeholder="please enter question stackLimit..."
              mode="button"
              size="large"
              v-model="form.judgeConfig.stackLimit"
            />
          </a-form-item>
        </a-space>
      </a-form-item>

      <a-form-item
        v-for="(judgeCaseItem, index) of form.judgeCase"
        :field="`form.judgeCase[${index}].input`"
        :label="`Post-${index}`"
        :key="index"
        style="align-items: center; vertical-align: center"
      >
        <a-form-item
          :field="`form.judgeCaseItem.input`"
          :label="`input`"
          :key="index"
        >
          <a-input
            v-model="judgeCaseItem.input"
            placeholder="please enter your input..."
          />
        </a-form-item>

        <a-form-item
          :field="`form.judgeCaseItem.output`"
          :label="`output`"
          :key="index"
        >
          <a-input
            v-model="judgeCaseItem.output"
            placeholder="please enter your output..."
          />
        </a-form-item>

        <a-button
          @click="handleDelete(index)"
          status="danger"
          style="margin-bottom: 20px; margin-left: 20px"
          >Delete</a-button
        >
      </a-form-item>

      <div>
        <a-button
          @click="handleAdd"
          status="success"
          style="margin-left: 334px; margin-bottom: 33px"
          >Add Example</a-button
        >
      </div>

      <a-form-item>
        <a-button @click="doSubmit">Submit</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, watch } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import {
  QuestionAddRequest,
  QuestionControllerService,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const updatePage = route.path.includes("update");

const form = ref({
  answer: "",
  content: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
  judgeConfig: {
    memoryLimit: 1000,
    stackLimit: 1000,
    timeLimit: 1000,
  },
  tags: [],
  title: "",
});

const handleAdd = () => {
  form.value.judgeCase?.push({
    input: "",
    output: "",
  });
};
const handleDelete = (index: any) => {
  form.value.judgeCase?.splice(index, 1);
};

const onAnswerChange = (val: string) => {
  form.value.answer = val;
};
const onContentChange = (val: string) => {
  form.value.content = val;
};

const doSubmit = async () => {
  // console.log(form);
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("update success!");
    } else {
      message.error("update failed! " + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );

    if (res.code === 0) {
      message.success("created success!");
    } else {
      message.error("created failed! " + res.message);
    }
  }
};

const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as any
  );
  if (res.code === 0) {
    message.success("loadData success!");
    form.value = res.data as any;
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }

    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      console.log(form.value.tags);
      form.value.tags = JSON.parse(form.value.tags as any);
    }

    console.log(res.data);
  } else {
    message.error("loadData failed!", res.message as any);
  }
};

onMounted(() => {
  loadData();

  // watch(
  //   () => form.value.tags,
  //   (newval, oldval) => {
  //     console.log(newval, oldval);
  //   },
  //   {
  //     deep: true,
  //   }
  // );
});
</script>
